package com.bookpavilion.order.controller;
import java.util.List;
import java.util.Map;

import com.bookpavilion.order.service.OrderItemService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import com.bookpavilion.order.pojo.Order;
import com.bookpavilion.order.service.OrderService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private OrderService orderService;

	@Autowired
	private OrderItemService orderItemService;
	/**
	 * 更新 更新时间
	 * @param id
	 * @return
	 */
	@PutMapping("/update/update_time/{id}")
	public Result updateUpdateTime(@PathVariable String id ){
		orderService.updateUpdateTimeById(id);
		return new Result(true,StatusCode.OK,"更新成功");
	}

	/**
	 * 更新付款时间
	 * @param id
	 * @return
	 */
	@PutMapping("/update/payment_time/{id}")
	public Result updatePaymentTime(@PathVariable String id){
		orderService.updatePaymentTimeById(id);
		return new Result(true,StatusCode.OK,"更新成功");
	}

	/**
	 * 更新交易完成时间
	 * @param id
	 * @return
	 */
	@PutMapping("/update/end_time/{id}")
	public Result updateEndTime(@PathVariable String id){

		orderService.updateEndTimeById(id);
		return new Result(true,StatusCode.OK,"更新成功");
	}
	/**
	 * 更新交易关闭时间
	 * @param id
	 * @return
	 */
	@PutMapping("/update/close_time/{id}")
	public Result updateCloseTime(@PathVariable String id){
		orderService.updateCloseTimeById(id);
		return new Result(true,StatusCode.OK,"更新成功");
	}
	/**
	 * 更新发货时间
	 * @param id
	 * @return
	 */
	@PutMapping("/update/consign_time/{id}")
	public Result updateConsignTime(@PathVariable String id){
		orderService.updateConsignTimeById(id);
		return new Result(true,StatusCode.OK,"更新成功");
	}

	/**
	 * 更新 物流信息
	 * @param shippingMsg
	 * @return
	 */
	@PutMapping("/update/shipping/{id}")
	public Result updateShippingMsg(@RequestBody Map<String,String> shippingMsg,HttpServletRequest request){
		Claims user_claims = (Claims) request.getAttribute("user_claims");
		if(user_claims==null)
			return new Result(false,StatusCode.ACCESSERROR,"权限不足");
		String id = shippingMsg.get("id");
		String shipping_name = shippingMsg.get("shipping_name");
		String shipping_code = shippingMsg.get("shipping_code");
		orderService.updateShippingMsg(id,shipping_name,shipping_code);
		return new Result(true,StatusCode.OK,"更新成功");
	}

	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(HttpServletRequest request){
		Claims user_claims = (Claims) request.getAttribute("user_claims");
		if(user_claims==null)
			return new Result(false,StatusCode.ACCESSERROR,"权限不足");
		return new Result(true,StatusCode.OK,"查询成功",orderService.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id,HttpServletRequest request){
		Claims user_claims = (Claims) request.getAttribute("user_claims");
		if(user_claims==null)
			return new Result(false,StatusCode.ACCESSERROR,"权限不足");
		return new Result(true,StatusCode.OK,"查询成功",orderService.findById(id));
	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size,HttpServletRequest request){
		Claims user_claims = (Claims) request.getAttribute("user_claims");
		if(user_claims==null)
			return new Result(false,StatusCode.ACCESSERROR,"权限不足");
		Page<Order> pageList = orderService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Order>(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap,HttpServletRequest request){
		Claims user_claims = (Claims) request.getAttribute("user_claims");
		if(user_claims==null)
			return new Result(false,StatusCode.ACCESSERROR,"权限不足");
        return new Result(true,StatusCode.OK,"查询成功",orderService.findSearch(searchMap));
    }
	
	/**
	 * 增加
	 * @param order
	 */

	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody Order order ,HttpServletRequest request){
		Claims user_claims = (Claims) request.getAttribute("user_claims");
		if(user_claims==null)
			return new Result(false,StatusCode.ACCESSERROR,"权限不足");
		orderService.add(order);
		return new Result(true,StatusCode.OK,"增加成功");
	}
	/**
	 * 修改
	 * @param order
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Order order, @PathVariable String id ,HttpServletRequest request){
		Claims user_claims = (Claims) request.getAttribute("user_claims");
		if(user_claims==null)
			return new Result(false,StatusCode.ACCESSERROR,"权限不足");
		order.setId(id);
		orderService.update(order);		
		return new Result(true,StatusCode.OK,"修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id,HttpServletRequest request ){
		Claims user_claims = (Claims) request.getAttribute("user_claims");
		if(user_claims==null)
			return new Result(false,StatusCode.ACCESSERROR,"权限不足");
		orderService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}
	
}
